package com.nexgo.payment.databases;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.nexgo.oaf.apiv3.device.pinpad.AlgorithmModeEnum;
import com.nexgo.oaf.apiv3.device.pinpad.PinPad;
import com.nexgo.payment.api.ApiDeviceEngine;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.util.EncryptUtils;
import com.nexgo.payment.util.ParamFileManager;
import com.nexgo.payment.util.StringUtils;

import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by xiaox on 2017/1/7.
 */

class AppParameterManager {
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    private AppParameterManager() {
    }

    public static AppParameterManager getInstance() {
        return SingletonCreate.getInstance;
    }

    private static class SingletonCreate {
        private static final AppParameterManager getInstance = new AppParameterManager();
    }

    public void init(Context context) {
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        mEditor = mSharedPreferences.edit();
        initParameter();
    }

    /**
     * 初始化表中的数据
     */
    private void initParameter() {
        //首次运行，初始化 5个操作员
        String initFlag = getValue(PreferencesConstants.INIT_FLAG);
        if (!initFlag.equals("1")) {
            //操作员信息
            String[] arrayOperator = new String[]{"01","02","03","04","05"};
            for(String operator : arrayOperator) {
                OperatorInfo user = new OperatorInfo();
                user.setOperatorNo(operator);
                user.setPassword(EncryptUtils.encryptSHA256ToString(StringUtils.leftPad("0", 4, '0')));
                Repository.getInstance().saveOperator(user);
            }
            setValue(PreferencesConstants.INIT_FLAG,"1");
        }
        //然后解析导入TempletPara.ini
        ParamFileManager.getInstance().parseTemplateParam();

        //国密
        if ("2".equals(getValue(PreferencesConstants.MASTER_KEY_ALGORITHM))) {
            PinPad pinPad = ApiDeviceEngine.getInstance().getPinPad();
            pinPad.setAlgorithmMode(AlgorithmModeEnum.SM4);
        }
        //签到日期
        setValue(PreferencesConstants.IS_NEED_LOGIN, "1");
        if ((new SimpleDateFormat("MMdd", SysConstant.LOCALE).format(new Date())).equals(getValue(PreferencesConstants.LOGIN_DATE))) {
            setValue(PreferencesConstants.IS_NEED_LOGIN, "0");
        }
        //保存初始化数据
        syncValue();
    }

    void setValue(String preferencesId, String val) {
        mEditor.putString(preferencesId, val);
    }

    String getValue(String preferencesId,String defValue) {
        return mSharedPreferences.getString(preferencesId, defValue);
    }
    String getValue(String preferencesId) {
        return mSharedPreferences.getString(preferencesId, "");
    }

    void syncValue() {
        mEditor.apply();
        try {
            Runtime.getRuntime().exec("sync");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void increaseTraceNumber() {
        NumberFormat format = NumberFormat.getInstance();
        format.setMinimumIntegerDigits(6);
        format.setGroupingUsed(false);
        String value;
        int traceNumber = Integer.parseInt(StringUtils.firstNonEmpty(getValue(PreferencesConstants.TRACE_NUMBER), "0")) + 1;
        if (traceNumber > 999999) {
            value = "000001";
        } else {
            value = format.format(traceNumber);
        }
        setValue(PreferencesConstants.TRACE_NUMBER, value);
        syncValue();
    }

    void increaseBatchNumber() {
        NumberFormat format = NumberFormat.getInstance();
        format.setMinimumIntegerDigits(6);
        format.setGroupingUsed(false);
        String value;
        int traceNumber = Integer.parseInt(StringUtils.firstNonEmpty(getValue(PreferencesConstants.BATCH_NUMBER), "0")) + 1;
        if (traceNumber > 999999) {
            value = "000001";
        } else {
            value = format.format(traceNumber);
        }
        setValue(PreferencesConstants.BATCH_NUMBER, value);
        syncValue();
    }
}